//
//  CXHToBidNativeAd.m
//  CXHAdSDK-ToBid
//
//  Created by 陈坤 on 2025/4/9.
//

#import "CXHToBidNativeAd.h"
#import <WindFoundation/WindFoundation.h>
#import <WindMillSDK/WindMillSDK.h>
#import <CXHAdSDK-Core/CXHAdSDKNativeAd.h>
#import "CXHToBidNativeAdData.h"
#import "CXHToBidNativeAdViewCreator.h"

@interface CXHToBidNativeAd ()<CXHAdSDKNativeAdDelegate>

@property (nonatomic, weak) id<AWMCustomNativeAdapterBridge> bridge;
@property (nonatomic, weak) id<AWMCustomNativeAdapter> adapter;

@property (nonatomic, strong) CXHAdSDKNativeAd *nativeAdsManager;

@end

@implementation CXHToBidNativeAd

- (instancetype)initWithBridge:(id<AWMCustomAdapterBridge>)bridge adapter:(id<AWMCustomAdapter>)adapter {
    self = [super init];
    if (self) {
        _bridge = (id<AWMCustomNativeAdapterBridge>)bridge;
        _adapter = (id<AWMCustomNativeAdapter>)adapter;
    }
    return self;
}

- (void)loadAdWithPlacementId:(NSString *)placementId adSize:(CGSize)size parameter:(AWMParameter *)parameter{
    CXHAdSDKNativeAd *nativeAd = [[CXHAdSDKNativeAd alloc] init];
    nativeAd.posId = placementId;
    nativeAd.delegate = self;
    self.nativeAdsManager = nativeAd;
    if (parameter.isHeaderBidding) {
        [self.nativeAdsManager load:1];
    }else {
        int count = [[parameter.extra objectForKey:AWMAdLoadingParamNALoadAdCount] intValue];
        [self.nativeAdsManager load:count];
    }
}

- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    if (result.win) {
        [self.nativeAdsManager sendWinNotice:result.winnerPrice];
        return;
    }
    // todo: 当胜出时，winnerPrice有值，其它情况不回传胜出价
    [self.nativeAdsManager sendLossNotice:result.winnerPrice];
}

#pragma mark - CXHAdSDKNativeAdDelegate
/**
 信息流原生广告请求成功

 @param nativeAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXH_nativeAdSucessToLoad:(CXHAdSDKNativeAd *)nativeAd
                     adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeAdViewDelegate> *> *)adViewArray{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    NSString *price = [NSString stringWithFormat:@"%ld", [self.nativeAdsManager getBidPrice]];
    [self.bridge nativeAd:self.adapter didAdServerResponseWithExt:@{
        AWMMediaAdLoadingExtECPM: price
    }];
    NSMutableArray *adArray = [[NSMutableArray alloc] init];
    for (UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView in adViewArray) {
        AWMMediatedNativeAd *mNativeAd = [[AWMMediatedNativeAd alloc] init];
        mNativeAd.data = [[CXHToBidNativeAdData alloc] initWithNativeAd:[nativeAdView cxh_nativeAdData]];
        mNativeAd.originMediatedNativeAd = nativeAdView;
        mNativeAd.viewCreator = [[CXHToBidNativeAdViewCreator alloc] initWithNativeAdView:nativeAdView];
        [adArray addObject:mNativeAd];
    }
    [self.bridge nativeAd:self.adapter didLoadWithNativeAds:adArray];
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXH_nativeAdFailToLoad:(CXHAdSDKNativeAd *)nativeAd
                         error:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didLoadFailWithError:error];
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClicked:(CXHAdSDKNativeAd *)nativeAd
                     adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didClickWithMediatedNativeAd:adView];
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClose:(CXHAdSDKNativeAd *)nativeAd
                   adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didClose:adView closeReasons:nil];
}

/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposure:(CXHAdSDKNativeAd *)nativeAd
                      adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge nativeAd:self.adapter didVisibleWithMediatedNativeAd:adView];
}


/**
 信息流广告展示上报
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposureReport:(CXHAdSDKNativeAd *)nativeAd
                            adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告点击上报

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClickedReport:(CXHAdSDKNativeAd *)nativeAd
                           adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告落地页被关闭
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdCloseLandingPage:(CXHAdSDKNativeAd *)nativeAd
                              adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}
- (void)dealloc {
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
}

@end
